package com.jkc.xh.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jkc.xh.R;
import com.jkc.xh.adapters.TradeAutoSoldPartAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.adapters.viewholder.HeadViewAutoSell;
import com.jkc.xh.adapters.viewholder.HeadViewAutoSell_;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.models.HoldOneModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.HoldOneResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_auto_sell)
public class TradeAutoSellFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {
    @ViewById(R.id.nav_bar)
    NavBar mNavBar;
    TradeAutoSoldPartAdapter mAdapter;
    private HeadViewAutoSell header;
    private int pageIndex = 0;
    private int pageTotal = 0;

    @Override
    protected void initView(PullRecyclerView recyclerView) {
        header = HeadViewAutoSell_.build(getContext());
        header.setHeadCallBack(headCallBack);
        mAdapter = new TradeAutoSoldPartAdapter(this);
        mAdapter.setItemClickListener(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        recyclerView.setHeaderView(header);
        mNavBar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                header.closeSoftInput();
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
                header.reloadStockInfo();
            }

        });
    }

    @AfterViews
    void initData() {
        requestData(true);
    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    private void requestData(final boolean isRefresh) {

        if (isRefresh) {
            pageIndex = 0;
            getPullRecyclerView().autoRefresh();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.queryHoldOneList(pageIndex,1, new RestCallBack<HoldOneResponse>() {
            @Override
            public void success(HoldOneResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
                    finishLoad(false);
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                    mAdapter.clearCurrentMoreView();
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {

            }
        }));


    }

    @Override
    public void onItemClick(View view, Object object, final int position) {
        HoldOneModel model = (HoldOneModel) object;
        switch (view.getId()) {
            case R.id.btnSoldPart:
                header.loadStockInfo(model.stock_code,model.id,model.enable_amount);
                getRecyclerView().smoothScrollToPosition(0);
                break;
        }
    }

    @Override
    public void onDetach() {
        header.clearAllJob();
        super.onDetach();
    }

    HeadViewAutoSell.HeadCallBack headCallBack = new HeadViewAutoSell.HeadCallBack() {

        @Override
        public void loadStart() {
            showProgress("开始加载");
        }

        @Override
        public void loadEnd() {
            hideProgress();
        }
    };


}
